package com.fjwt.gz.mch.config;

import com.fjwt.gz.core.cache.redis.RedisDataBaseContent;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Value("${redis.host}")
    private String hostName;
    @Value("${redis.port}")
    private int port;
    @Value("${redis.password}")
    private String password;

    @Value("${redis.jedis.pool.max-active}")
    private int maxTotal; //连接池中最大连接数
    @Value("${redis.jedis.pool.max-idle}")
    private int maxIdle; //最大空闲连接数
    @Value("${redis.jedis.pool.min-idle}")
    private int minIdle; //最小空闲连接数
    @Value("${redis.jedis.pool.max-wait}")
    private int maxWait; //当池内没有可用的连接时，最大等待时间
    @Value("${redis.jedis.pool.test-on-borrow}")
    private boolean testOnBorrow; //在获取连接时是否检查有效性
    @Value("${redis.jedis.pool.test-on-return}")
    private boolean testOnReturn; //在归还连接时是否检查有效性

    @Value("${redis.database.platform-db}")
    private int databasePlatform;
    @Value("${redis.database.statistics-db}")
    private int databasesTatistics;

    /**
     * 平台库
     * **/
    @Primary
    @Bean(name = RedisDataBaseContent.REDIS_TEMPLATE_PLATFORM)
    public RedisTemplate<String, Object> platformRedisTemplate() {
        return getRedisTemplate(databasePlatform);
    }

    /**
     * 统计库
     * **/
    @Bean(name = RedisDataBaseContent.REDIS_TEMPLATE_STATISTICS)
    public RedisTemplate<String, Object> statisticsRedisTemplate() {
        return getRedisTemplate(databasesTatistics);
    }

    private RedisTemplate<String, Object> getRedisTemplate(int database) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(getLettuceConnectionFactory(database));
        /** key采用StringRedisSerializer序列化 */
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        /** 设置hash key和value的序列化方式 */
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }

    /**
     * 创建连接池
     *
     * @param database
     * @return
     */
    public LettuceConnectionFactory getLettuceConnectionFactory(int database) {
        LettuceConnectionFactory factory = new LettuceConnectionFactory(getRedisStandaloneConfig(database), getLettuceClientConfiguration());
        // 重新初始化工厂
        factory.afterPropertiesSet();
        return factory;
    }

    private RedisStandaloneConfiguration getRedisStandaloneConfig(int database) {
        RedisStandaloneConfiguration redisStandaloneConfig = new RedisStandaloneConfiguration();
        redisStandaloneConfig.setHostName(hostName);
        redisStandaloneConfig.setPort(port);
        redisStandaloneConfig.setPassword(password);
        redisStandaloneConfig.setDatabase(database);
        return redisStandaloneConfig;
    }

    private LettuceClientConfiguration getLettuceClientConfiguration() {
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxIdle(maxIdle);
        genericObjectPoolConfig.setMinIdle(minIdle);
        genericObjectPoolConfig.setMaxTotal(maxTotal);
        genericObjectPoolConfig.setMaxWaitMillis(maxWait);
        genericObjectPoolConfig.setTestOnBorrow(testOnBorrow);
        genericObjectPoolConfig.setTestOnReturn(testOnReturn);

        return LettucePoolingClientConfiguration.builder()
                .poolConfig(genericObjectPoolConfig)
                .build();

    }
}
